package rin.android.gamble;


import java.util.ArrayList;


import rin.android.database.ClassPlayer;
import rin.android.database.Database;
import rin.android.dialog.MessageDialog;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Login extends MyApp{
	ArrayList<ClassPlayer> listPlayer;
	EditText name;
	EditText pass;
	MessageDialog dialog;
	Context context;
	Database mData;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        dialog = new MessageDialog();
        mData = new Database(this);
        name = (EditText) findViewById(R.id.log_name);
        pass = (EditText) findViewById(R.id.log_pass);
        TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
        Button login = (Button) findViewById(R.id.btnLogin);
        Button thoat = (Button) findViewById(R.id.btthoat);
        context = this;
        // Listening to register new account link
        registerScreen.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// Switching to Register screen
				Intent i = new Intent(getApplicationContext(), Register.class);
				startActivity(i);
			}
		});
        
        login.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String userName = name.getText().toString().trim();
				String mk = pass.getText().toString().trim();
				if(userName.equals("")){
					dialog.showNullDialog(context);
					return;
				}
				if(pass.equals("")){
					dialog.showNullDialog(context);
					return;
				}
				if(mData.isPlayer(userName, mk) == 1){
					Intent i = new Intent(getApplicationContext(), MainActivity.class);
					i.putExtra("userName", userName);
					startActivity(i);
				}
				else if(mData.isPlayer(userName, mk) == 0){
					dialog.showLoginFailedDialog(context);
				}
			}
		});
        
        thoat.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
        	
        });
    }
	public int isPlayer(){
		String userName = name.getText().toString().trim();
		String mk = pass.getText().toString().trim();
		if(userName.equals("")){
			dialog.showNullDialog(context);
			return 2;
		}
		if(pass.equals("")){
			dialog.showNullDialog(context);
			return 3;
		}
		for(int i =0;i<listPlayer.size();i++){
			if(userName.equals(listPlayer.get(i).getName()) && mk.equals(listPlayer.get(i).getPass()))return 1;
		}
		return 0;
	}
	public void onResume(){
		super.onResume();
		name.setText("");
		pass.setText("");
		mData.openDatabase();
	}
	public void onDestroy(){
		super.onDestroy();
		mData.close();
	}
	
}

